package com.totem.admin.controller;

import cn.hutool.crypto.asymmetric.RSA;
import com.totem.admin.api.AdminServiceApi;
import com.totem.base.constants.CommonConstants;
import com.totem.base.constants.SecurityComponets;
import com.totem.base.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class AdminController implements AdminServiceApi {
    private static final boolean encryptEnable = CommonConstants.ENCRYPT_ENABLE;
    private static final RSA rsa = SecurityComponets.DEFAULT_ENCRYPTOR;
    @Override
    public Result systemConfig() {
        String publicKeyBase64 = rsa.getPublicKeyBase64();
        if(log.isDebugEnabled()) {
            log.debug("encryptEnable:{}, publicKey:{}", encryptEnable, publicKeyBase64);
        }
        String pemPublicKey = "-----BEGIN PUBLIC KEY-----\n";
        pemPublicKey +=publicKeyBase64;
        pemPublicKey += "\n-----END PUBLIC KEY-----\n";
        Map<String, Object> systemConfig = new HashMap<>();
        systemConfig.put("isEncrypt", encryptEnable);
        systemConfig.put("publicKey",pemPublicKey);
        return Result.success(systemConfig);
    }

    @Override
    public Result appConfig() {
        return Result.success();
    }
}

